package mobile.TheOldRepublic.web;

import mobile.TheOldRepublic.util.Constants;

import com.explodingzombie.util.GeneralUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.Toast;

public class WebViewActivity extends Activity {
	
	private WebViewWrapper webview = null;
	private String url = null;

	@Override
    protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		webview = new WebViewWrapper(this);
		if(savedInstanceState != null){
			webview.restoreState(savedInstanceState);
		}
		
		Bundle extras = getIntent().getExtras();
		if(extras != null)
			url = extras.getString(Constants.KEY_URL);
		if(GeneralUtils.isEmpty(url)){
			this.finish();
			return;
		}
		
		System.out.println("Loading URL "+url);
		setContentView(webview);
		webview.loadUrl(url);
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState){
		webview.saveState(outState);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
		if((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()){
			Log.d(this.getClass().getSimpleName(), "Pressing back button.");
			webview.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private OnMenuItemClickListener refreshListener = new OnMenuItemClickListener() {
		@Override
		public boolean onMenuItemClick(MenuItem item) {
			// We had images that were stuck having not been loaded.
			// To fix I'm clearing the cache on reload. LP
			Log.d(this.getClass().getSimpleName(), "Clearing cache on refresh.");
			webview.clearCache(true);
			Log.d(this.getClass().getSimpleName(), "Reloading page.");
//			webview.reload();
			webview.loadUrl(url);
			return true;
		}
	};
	    
    @Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		final Context context = this;
		menu.clear();
		
		MenuItem refresh = menu.add("Refresh");
		refresh.setIcon(android.R.drawable.ic_menu_rotate);
		refresh.setOnMenuItemClickListener(refreshListener);
		
		MenuItem sendDebug = menu.add("Send HTML to Debug");
		sendDebug.setIcon(android.R.drawable.ic_menu_send);
		sendDebug.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				try{
					String outHtml = webview.getDebuggingHtml();
					Intent i = new Intent(Intent.ACTION_SEND);
					i.setPackage("com.google.android.gm");
					i.setType("text/html");
					i.putExtra(Intent.EXTRA_EMAIL, new String[]{"ittr-bugs@explodingzombie.com"});
					i.putExtra(Intent.EXTRA_SUBJECT, "ITTR Test HTML");
					i.putExtra(Intent.EXTRA_TEXT, "<!-- "+url+" -->"+outHtml);
					context.startActivity(i);
				}catch(Exception e){
					e.printStackTrace();
					Toast.makeText(context, "Failed to send email.", Toast.LENGTH_LONG).show();
				}
				return true;
			}
		});
				
		return true;
	}
}